cmake_minimum_required(VERSION 3.16)

project(obj_det)

include(${PROJECT_SOURCE_DIR}/cmake/common.cmake)

message(STATUS "TensorRT_ROOT:${TensorRT_ROOT}")

# set(TensorRT_ROOT /workplace/detect_track/TensorRT-8.4.2.4)

add_subdirectory(${PROJECT_SOURCE_DIR}/yolov8-deepsort-tensorrt)

add_subdirectory(${PROJECT_SOURCE_DIR}/live555/source)

# gather TensorRT include
find_path(TENSORRT_INCLUDE_DIR NAMES NvInfer.h HINTS ${TensorRT_ROOT} PATH_SUFFIXES include)
set(SAMPLES_COMMON_DIR ${TensorRT_ROOT}/samples/common)
list(APPEND TENSORRT_INCLUDE_DIR ${SAMPLES_COMMON_DIR})

find_package(OpenCV REQUIRED)

# if(NOT TARGET flatbuffers)
#   find_package(flatbuffers CONFIG REQUIRED)
# endif()

# if(NOT TARGET cppzmq)
#   find_package(cppzmq CONFIG REQUIRED)
# endif()


if(NOT TARGET nlohmann_json)
  find_package(nlohmann_json CONFIG REQUIRED)
endif()

find_package(CUDA)
find_library(CUVID_LIB nvcuvid)

include_directories(
  ${PROJECT_SOURCE_DIR}/include
  ${PROJECT_SOURCE_DIR}/include/frame_parser
  ${PROJECT_SOURCE_DIR}/include/stream_decode
  ${PROJECT_SOURCE_DIR}/src/NvDecoder/include
  ${PROJECT_SOURCE_DIR}/live555/source/liveMedia/include
  ${PROJECT_SOURCE_DIR}/live555/source/liveMedia
  ${PROJECT_SOURCE_DIR}/live555/source/groupsock/include
  ${PROJECT_SOURCE_DIR}/live555/source/UsageEnvironment/include
  ${PROJECT_SOURCE_DIR}/live555/source/BasicUsageEnvironment/include
  ${PROJECT_SOURCE_DIR}/src/base
  ${PROJECT_SOURCE_DIR}/yolov8-deepsort-tensorrt/include
  ${PROJECT_SOURCE_DIR}/yolov8-deepsort-tensorrt/yolov8
  ${PROJECT_SOURCE_DIR}/yolov8-deepsort-tensorrt/deepsort/include
  ${OpenCV_INCLUDE_DIRS}
  ${TENSORRT_INCLUDE_DIR}
)

link_directories(
	${PROJECT_SOURCE_DIR}/live555/target/lib
)

link_libraries(
  ${OpenCV_LIBS}
  ${CUDA_CUDA_LIBRARY} 
  ${CUVID_LIB}
  ${CUDA_nppc_LIBRARY}
  ${CUDA_nppicc_LIBRARY}
  ${CUDA_nppisu_LIBRARY}
  ${CUDA_nppig_LIBRARY}
  deepsort
  yolov8
  liveMedia 
  BasicUsageEnvironment 
  groupsock 
  UsageEnvironment
  nlohmann_json::nlohmann_json
  # flatbuffers::flatbuffers
  libssl.a
  libcrypto.a
  dl
  z
  lzma
  pthread
)

file(GLOB_RECURSE SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/*.cpp ${PROJECT_SOURCE_DIR}/src/*.c)
list(APPEND SOURCE_FILE ${PROJECT_SOURCE_DIR}/yolov8-deepsort-tensorrt/src/manager.cpp)

set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-gencode arch=compute_50,code=\"sm_50,compute_50\")
if ( CMAKE_COMPILER_IS_GNUCC )
    if(NOT "${CUDA_NVCC_FLAGS}" MATCHES "-std=c\\+\\+11" )
        list(APPEND CUDA_NVCC_FLAGS -std=c++11)
    endif()
endif()

cuda_add_executable(obj_det  ${SOURCE_FILE})

add_dependencies(obj_det yolov8)
add_dependencies(obj_det deepsort)